/*********** tuannh ****************
 function : vector function
***********************************/
function Struct_Data(_names) {
    var names = _names.split('|');
    var count = names.length;
    function constructor() {
        for (var i = 0; i < count; i++) {
            this[names[i]] = arguments[i];
        }
    }
    return constructor;
}

var Point_Vector_Decac = Struct_Data("x|y");
// type_vector: X : truong hop x = a voi moi y
// type_vector: Y : truong hop y = b voi moi X
// type_vector: XY : truong hop y = ax + b
var Line_Vector_Decac = Struct_Data("a|b|length_vector|type_vector");

var line1 = new Line_Vector_Decac(2, 3, 100, 'XY');
var line2 = new Line_Vector_Decac(3, 6, 100, 'XY');

function USCLN(x, y) {
    if (x % y == 0) return y;
    var USC = x;
    while (x != 0 && y != 0) {
        x = x % y;
        if (x != 0) {
            // x : so nho - y : so lon
            y = y % x;

            if (y == 0) {
                USC = x;
                break;
            }
        }
    }
    return USC;
}

function BSCNN(x, y) {
    if (x % y == 0) return x;
    var usc = USCLN(x, y);
    return x * y / usc;
}

// tinh do dai vector qua 2 diem
function lengthXY(point1, point2) {
    var dLength = 0;
    var dX = Math.pow(point1.x - point2.x, 2);
    var dY = Math.pow(point1.y - point2.y, 2);
    dLength = Math.sqrt(dX + dY);
    return dLength;
}

// duong thang di qua 2 diem point1, point2
function create_Vector(point1, point2) {
    // truong hop x = a voi moi y
    var vector_Line = null;
    var a, b, length_vector;
    if (point1.x == point2.x) {
        b = 0;
        a = point1.x;
        length_vector = Math.abs(point1.y - point2.y);
        vector_Line = new Line_Vector_Decac(a, b, length_vector, "X");
        return vector_Line;
    }
    // truong hop y = b voi moi x
    if (point1.y == point2.y) {
        a = 0;
        b = point1.y;
        length_vector = Math.abs(point1.x - point2.x);
        vector_Line = new Line_Vector_Decac(a, b, length_vector, "Y");
        return vector_Line;
    }

    // d1.X = 2 - d1.Y = 3
    // d2.X = 3 - di.Y = 4
    var dBSCNN = BSCNN(point1.x, point2.x); // 6
    var deltaX1 = dBSCNN / point1.x; // 3
    var deltaX2 = dBSCNN / point2.x; // 2
    var deltaX1_X2 = deltaX1 - deltaX2; // 1
    var dB = deltaX1 * point1.y - deltaX2 * point2.y; // 3 * 3 - 2 * 4 = 1
    dB = dB / deltaX1_X2; // 1 / 1 = 1

    var dA = (point1.y - dB) / point1.x;

    a = dA;
    b = dB;
    length_vector = lengthXY(point1, point2);
    vector_Line = new Line_Vector_Decac(a, b, length_vector, "XY");
    return vector_Line;
}

// duong thang di qua 1 diem va song song voi 1 duong khac
function create_Vector_Parallel(point1, line1) {
    var vector_Line = null;
    var a, b, length_vector;
    // truong hop x = a voi moi y
    if (line1.XY == 'X') {
        b = 0;
        a = point1.x;
        length_vector = 1000; // default
        vector_Line = new Line_Vector_Decac(a, b, length_vector, "X");
        return vector_Line;
    }
    // truong hop y = b voi moi x
    if (line1.XY == 'Y') {
        a = 0;
        b = point1.y;
        length_vector = 1000; // default
        vector_Line = new Line_Vector_Decac(a, b, length_vector, "Y");
        return vector_Line;
    }
    a = line1.a;
    b = point1.y - point1.x * a;
    length_vector = 1000; // default
    vector_Line = new Line_Vector_Decac(a, b, length_vector, "XY");
    return vector_Line;
}

// tim giao diem cua 2 duong
function find_Point_From2Line(line1, line2) {
    var point = new Point_Vector_Decac('0', '0');
    if (line1.a == line2.a) {
        alert('2 dường thảng song song nên không có giao điểm.');
        return point;
    }

    // line1: y = 10
    // line2: y = 3*x + 5
    if (line1.a == 0) {
        point.x = (line1.b - line2.b) / line2.a;
        point.y = line1.b;
        return point;
    }

    // line2: y = 3*x + 5
    // line1: y = 10
    if (line2.a == 0) {
        point.x = (line2.b - line1.b) / line1.a;
        point.y = line2.b;
        return point;
    }

    // line1: x = 10
    // line2: y = 3*x + 5
    if (line1.b == 0) {
        point.x = line1.a;
        point.y = line2.a * line1.a + line2.b;
        return point;
    }

    // line2: y = 3*x + 5
    // line1: x = 10
    if (line2.b == 0) {
        point.x = line2.a;
        point.y = line1.a * line2.a + line1.b;
        return point;
    }

    // line1: y = 2*x + 3
    // line2: y = 3*x + 5
    var dBSCNN = BSCNN(line1.a, line2.a); // 6
    var deltaX1 = dBSCNN / line1.a; // 3
    var deltaX2 = dBSCNN / line2.a; // 2
    var deltaX1_X2 = deltaX1 - deltaX2; // 1
    // tim y
    var dY = deltaX1 * line1.b - deltaX2 * line2.b; // 3 * 3 - 2 * 5 = -1
    dY = dY / deltaX1_X2; // -1 / 1 = -1
    // tim x
    var dX = (dY - line1.b) / line1.a;
    // xac dinh toa do diem giao cua 2 duong
    point.x = dX;
    point.y = dY;
    return point;
}

// tinh goc cua duong thang so voi truc OX
function Goc_DuongThang(vector_Line) {
    var dX = vector_Line.b * -1 / vector_Line.a;
    var dY = vector_Line.b;
    if (vector_Line.type_vector=="Y") {
        return 0;
    }
    if (vector_Line.type_vector == "X") {
        return 90;
    }
    if (dX == 0 && dY == 0) {
        return 45;
    }
    var tan = 0;
    var goc = 0;
    tan = dY / dX;
    // atan=angle*PI/180  
    goc = Math.atan(tan) * 180 / Math.PI;
    if (dX < 0 && dY < 0) {
        goc = 360 - goc;
    }
    else if (dX < 0 && dY > 0) {
        if (goc < 0) goc = goc * (-1) + 180;
    }
    else if (dX > 0 && dY < 0) {
        if (goc < 0) goc = goc * (-1) + 180;
    }
    else if (dX > 0 && dY > 0) {
        goc = 180 - goc;
    }
    return goc;
}
/****** end function *********/